#include "clickedlabel.h"
#include <QMouseEvent>
#include <QEvent>
#include <QDebug>

ClickedLabel::ClickedLabel(QWidget * parent)
:QLabel(parent),curstate_(ClickLbState::Normal)
{
    this->setCursor(Qt::PointingHandCursor);
}

void ClickedLabel::enterEvent(QEnterEvent *event)
{
    qDebug()<<"ClickedLabel::enterEvent\n";
    if(curstate_==ClickLbState::Normal)
    {
        setProperty("state",normal_hover_);
    }
    else
    {
        setProperty("state",selected_hover_);
    }
    repolish(this);
    update();

    QLabel::enterEvent(event);
}

void ClickedLabel::leaveEvent(QEvent *event)
{
    qDebug()<<"ClickedLabel::leaveEvent\n";
    if(curstate_==ClickLbState::Normal)
    {
        setProperty("state",normal_);
    }
    else
    {
        setProperty("state",selected_);
    }

    repolish(this);
    update();

    QLabel::leaveEvent(event);
}
//本项目中press按下后用的是hover的图片来替代本该是press的图片
void ClickedLabel::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        if(curstate_ == ClickLbState::Normal)
        {
            qDebug()<<"clicked，change to selected hover"<<selected_hover_;
            curstate_=ClickLbState::Selected;
            this->setProperty("state",selected_hover_);

        }
        else
        {
            qDebug()<<"clicked，change to normal hover"<<normal_hover_;
            curstate_=ClickLbState::Normal;
            this->setProperty("state",normal_hover_);

        }
        repolish(this);
        update();
        emit clicked();
    }

    QLabel::mousePressEvent(event);
}

void ClickedLabel::SetState(QString normal, QString hover, QString press, QString select, QString select_hover, QString select_press)
{
    normal_=normal;
    normal_hover_=hover;
    normal_press_ = press;

    selected_ = select;
    selected_hover_ = select_hover;
    selected_press_ = select_press;

    setProperty("state",normal_);
    repolish(this);
    update();
}

ClickLbState ClickedLabel::GetCurState()
{
    return curstate_;
}

